package perceptrons;

public class Perceptron 
{
	private class ErrorClass
	{
		int errorResult;
		TrainingSet set;
	}
	
	private TrainingSet[] trainingsets;
	private int[] weights;
	private int bias;
	
	public Perceptron(int bias, int[] weights, TrainingSet... sets)
	{
		assert weights.length-1 == sets[0].inputs.length;
		
		this.bias = bias;
		this.weights = weights;
		this.trainingsets = sets;
	}
	
	public boolean learn(int rate)
	{
		boolean hasLearned = false;
		
		while(!hasLearned)
		{
			if(finish())
			{
				hasLearned = true;
			}
			
		}
		
		return hasLearned;
	}
	
	private boolean finish()
	{
		boolean isCorrect = true;
		ErrorClass error = new ErrorClass();
		
		for(TrainingSet set : trainingsets)
		{
			int testOutput = bias*weights[0];
			
			for(int i = 0; i < weights.length-1; i++)
			{
				testOutput = testOutput + weights[i+1]*set.inputs[i];
			}
			
			if(testOutput > 0)
			{
				if(set.output <= 0)
				{
					isCorrect = false;
					error.errorResult = testOutput;
					error.set = set;
					break;
				}
			}
			else
			{
				if(set.output > 0)
				{
					isCorrect = false;
					error.errorResult = testOutput;
					error.set = set;
					break;
				}
			}
		}
		
		if(!isCorrect)
		{
			correctIt(error);
		}
		
		return isCorrect;
	}
	
	private void correctIt(ErrorClass err)
	{
		//TODO
	}
}
